package com.byb.server.service.impl;

import com.byb.server.service.IsurerService;
import com.byb.server.domain.Isurer;
import com.byb.server.repository.IsurerRepository;
import com.byb.server.repository.search.IsurerSearchRepository;
import com.byb.server.service.dto.IsurerDTO;
import com.byb.server.service.mapper.IsurerMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import static org.elasticsearch.index.query.QueryBuilders.*;

/**
 * Service Implementation for managing Isurer.
 */
@Service
@Transactional
public class IsurerServiceImpl implements IsurerService{

    private final Logger log = LoggerFactory.getLogger(IsurerServiceImpl.class);

    private final IsurerRepository isurerRepository;

    private final IsurerMapper isurerMapper;

    private final IsurerSearchRepository isurerSearchRepository;
    public IsurerServiceImpl(IsurerRepository isurerRepository, IsurerMapper isurerMapper, IsurerSearchRepository isurerSearchRepository) {
        this.isurerRepository = isurerRepository;
        this.isurerMapper = isurerMapper;
        this.isurerSearchRepository = isurerSearchRepository;
    }

    /**
     * Save a isurer.
     *
     * @param isurerDTO the entity to save
     * @return the persisted entity
     */
    @Override
    public IsurerDTO save(IsurerDTO isurerDTO) {
        log.debug("Request to save Isurer : {}", isurerDTO);
        Isurer isurer = isurerMapper.toEntity(isurerDTO);
        isurer = isurerRepository.save(isurer);
        IsurerDTO result = isurerMapper.toDto(isurer);
       // isurerSearchRepository.save(isurer);
        return result;
    }

    /**
     *  Get all the isurers.
     *
     *  @param pageable the pagination information
     *  @return the list of entities
     */
    @Override
    @Transactional(readOnly = true)
    public Page<IsurerDTO> findAll(Pageable pageable) {
        log.debug("Request to get all Isurers");
        return isurerRepository.findAll(pageable)
            .map(isurerMapper::toDto);
    }

    /**
     *  Get one isurer by id.
     *
     *  @param id the id of the entity
     *  @return the entity
     */
    @Override
    @Transactional(readOnly = true)
    public IsurerDTO findOne(Long id) {
        log.debug("Request to get Isurer : {}", id);
        Isurer isurer = isurerRepository.findOne(id);
        return isurerMapper.toDto(isurer);
    }

    /**
     *  Delete the  isurer by id.
     *
     *  @param id the id of the entity
     */
    @Override
    public void delete(Long id) {
        log.debug("Request to delete Isurer : {}", id);
        isurerRepository.delete(id);
       // isurerSearchRepository.delete(id);
    }

    /**
     * Search for the isurer corresponding to the query.
     *
     *  @param query the query of the search
     *  @param pageable the pagination information
     *  @return the list of entities
     */
    @Override
    @Transactional(readOnly = true)
    public Page<IsurerDTO> search(String query, Pageable pageable) {
        log.debug("Request to search for a page of Isurers for query {}", query);
        Page<Isurer> result = isurerSearchRepository.search(queryStringQuery(query), pageable);
        return result.map(isurerMapper::toDto);
    }
}
